package com.mmp.main.servlet;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.Message;
import com.mmp.common.bo.Message.MESSAGE_TYPE;
import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.JobOrder;
import com.mmp.main.util.Default;
import com.mmp.main.util.WebUtil;

@WebServlet(urlPatterns = { "/CancelJobOrder", "/FollowupJobOrder" })
public class SearchForJobOrderProcessServlet extends BasicServlet {

	private static HashMap<String, String> destinations;

	static {
		destinations = new HashMap<String, String>();
		destinations.put("CancelJobOrder", "JobOrderDeleteDisplay");
		destinations.put("FollowupJobOrder", "JobOrderTrackingDisplay");
		destinations.put("JobOrderFollowupFirstPhaseProcess", "JobOrderTrackingDisplay");
		destinations.put("JobOrderFollowupSecondPhaseProcess", "JobOrderTrackingDisplay");
		destinations.put("JobOrderFollowupThirdPhaseProcess", "JobOrderTrackingDisplay");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String orderNo = WebUtil.getParamValue(request, "jobOrderNo", null);
		String sn = WebUtil.getParamValue(request, "deviceSerial", null);
		String code = WebUtil.getParamValue(request, "deviceCode", null);
		int siteId = getSessionSiteId(request);
		int locationId = getSessionLocationId(request);

		JobOrder order = dbQueryManager.searchForActiveJobOrder(siteId, locationId, orderNo, code, sn);

		if (order != null) {
			request.setAttribute(Default.JOB_ORDER_INFO_ATTR_NAME, order);
			request.setAttribute(Default.DEVICE_INFO_ATTR_NAME, order.getHospitalDevice());
			basicForward(request, response, getSearchDestination(destinations, request));
			return;
		}

		String msg = "لم يتم العثور علي أي نتائج موافقة لبحثك";

		if (WebUtil.getParamValue(request, "report", null) != null) {
			announceErorrMessage(request, response, msg);
		} else {
			setMessage(request, new Message(msg, MESSAGE_TYPE.ERROR));
			basicForward(request, response, WebUtil.getSearchDisplayURL(request));
		}
	}
}
